<template>
  <div class="content">
    <div class="header">
      <h3 class="title">用户列表</h3>
      <el-button type="primary" @click="handleNewUserClick">新建数据</el-button>
    </div>
    <div class="table">
      <el-table :data="usersList" style="width: 100%">
        <el-table-column type="selection"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column label="用户名" prop="name"></el-table-column>
        <el-table-column label="真实姓名" prop="realname"></el-table-column>
        <el-table-column label="手机号" prop="cellphone"></el-table-column>
        <el-table-column label="状态" prop="enable">
          <template #default="scope">
            <el-button size="small" :type="scope.row.enable ? 'success' : 'danger'" plain>
              {{ scope.row.enable ? '启用' : '禁用' }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createAt">
          <template #default="scope">
            {{ formatUTC(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="updateAt">
          <template #default="scope">
            {{ formatUTC(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEditBtnClick(scope.row)"
              >编辑</el-button
            >
            <el-button size="small" type="danger" text @click="handleDeleteBtnClick(scope.row.id)"
              >删除</el-button
            ></template
          >
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="usersTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { formatUTC } from '@/utils/format'
import { storeToRefs } from 'pinia'
import useSystemStore from '@/store/main/system/system'

// 定义事件用来抛发弹窗事件
const emit = defineEmits(['newBtnClick', 'editBtnClick'])

// 1. 发起action,请求列表数据
const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = useSystemStore()
fetchUserListData()

// 2. 获取数据
// storeToRefs 是一个响应式的
const { usersList, usersTotalCount } = storeToRefs(systemStore)

// 3. 页码校验

function handleSizeChange() {
  console.log('pageSize', pageSize.value)
  fetchUserListData()
}
function handleCurrentChange() {
  console.log('currentPage', currentPage.value)
  fetchUserListData()
}

// 4. 定义函数,用于发送网络请求
function fetchUserListData(formData: any = {}) {
  console.log(pageSize.value, currentPage.value, '--v')
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset } // 页码

  const queryInfo = { ...pageInfo, ...formData } // 搜索框

  systemStore.postUsersListAction(queryInfo)
}

// 5. 删除功能
function handleDeleteBtnClick(id: number) {
  console.log('删除', id)
  systemStore.deleteUsersByIdAction(id)
}

// 6. 新建用户功能
function handleNewUserClick() {
  console.log('新建用户')
  emit('newBtnClick')
}
function handleEditBtnClick(itemData: any) {
  console.log('编辑用户', itemData)
  emit('editBtnClick', itemData)
}

// 暴露网络请求的方式,这样就可以通过ref访问了
defineExpose({
  fetchUserListData
})
</script>

<style scoped lang="less">
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}
.header {
  display: flex;
  justify-content: space-between;
  h3 {
    margin: 0;
    padding: 0;
    font-size: 22px;
  }
  .title {
    // font-size: 30px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
